package paravyan;

public class QuickSort {
	public static int divide(int[] array, int left, int right) {
		int i = left;
		int j = right;
		int element = array[(left + right) / 2];

		while (i <= j) {
			while (array[i] < element)
				++i;
			while (array[j] > element)
				--j;
			if (i <= j) {
				swap(array, i, j);
				++i;
				--j;
			}
		}
		;
		return i;
	}

	public static void quickSort(int[] array, int left, int right) {
		int index = divide(array, left, right);
		if (left < index - 1)
			quickSort(array, left, index - 1);
		if (index < right)
			quickSort(array, index, right);
	}

	private static void swap(int[] array, int index1, int index2) {
		int tmp = array[index1];
		array[index1] = array[index2];
		array[index2] = tmp;
	}

}
